package com.example.demo.controller;


import com.example.demo.anno.Log;
import com.example.demo.common.Result;

import com.example.demo.pojo.PageBean;
import com.example.demo.pojo.User;
import com.example.demo.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
//springCache+redis
public class LonginController {
    public String account;//定义给过滤器用的


    @Autowired
    private UserService userService;

    //登录操作,查询用户名密码 学生
    //这里的session已经写进去了,读不出来是返回的问题,即所有地方都要写withCredentials:true,
    @Log
    @PostMapping("/loginStu")
    public Result selectUser(@RequestBody User user,HttpServletRequest request){
        List<User> userList = userService.selectUser();
        for (int i = 0; i < userList.size() ; i++){
            if(userList.get(i).getAccount().equals(user.getAccount())){
                if (userList.get(i).getPassword().equals(user.getPassword())){
                    request.getSession().setAttribute("user",user.getAccount());//存给过滤器登录校验用的
                    return Result.success(user.getAccount());
                }else{
                    return Result.error("请输入正确的密码");
                }
            }
        }

       return Result.error("账户不存在");
    }
//    @GetMapping("/login stu")
//    public String selectUser(){
//        return "student.html";
//    }
    //注册
    @Log
    @PostMapping("/registerStu")
    public Result save(@RequestBody User user){
        userService.save(user);
        return Result.success();
    }

    //登录操作,查询用户名密码 企业
    @Log
    @PostMapping("/loginCompany")
    public Result selectUserCm(@RequestBody User user,HttpServletRequest request){
        List<User> userList = userService.selectUserCm();
        for (int i = 0; i < userList.size() ; i++){
            if(userList.get(i).getAccount().equals(user.getAccount())){
                if (userList.get(i).getPassword().equals(user.getPassword())){
                    request.getSession().setAttribute("user",user.getAccount());//存给过滤器登录校验用的
                    return Result.success(user.getAccount());
                }else{
                    return Result.error("请输入正确的密码");
                }
            }
        }

        return Result.error("账户不存在");
    }
    //注册
    @Log
    @PostMapping("/registerCompany")
    public Result saveCm(@RequestBody User user){
        userService.saveCm(user);
        return Result.success();
    }

    //登录操作,查询用户名密码 管理员
    @Log
    @PostMapping("/loginAdmin")
    public Result selectUserAd(@RequestBody User user,HttpServletRequest request){
        List<User> userList = userService.selectUserAd();
        for (int i = 0; i < userList.size() ; i++){
            if(userList.get(i).getAccount().equals(user.getAccount())){
                if (userList.get(i).getPassword().equals(user.getPassword())){
                    request.getSession().setAttribute("user",user.getAccount());//存给过滤器登录校验用的
                    return Result.success(user.getAccount());
                }else{
                    return Result.error("请输入正确的密码");
                }
            }
        }

        return Result.error("账户不存在");
    }
    //注册
    @PostMapping("/registerAdmin")
    public Result saveAd(@RequestBody User user){
        userService.saveAd(user);
        return Result.success();
    }



    //查询用户名密码 学生 分页查询
    @GetMapping("/login stu")
    public Result selectUser(
            Integer pageNum,//当前页码
            Integer pageSize,//每页条数
            @RequestParam(required = false) Integer categoryId,//分类id  ↓状态(发布)
            @RequestParam(required = false) String state){
         PageBean<User> pb =  userService.list(pageNum,pageSize);
        return Result.success(pb);
    }

    //查询用户名密码 企业
    @GetMapping("/login company")
    public Result selectUserCm(HttpServletRequest httpServletRequest){
        List<User> userList = userService.selectUserCm();
        return Result.success(userList);
    }

    //删除学生账号
    @DeleteMapping("/deleteStu/{account}")
    public Result deleteStu(@PathVariable String account)  {
        userService.deleteStu(account);
        return Result.success("删除成功");
    }

    //删除员工账户
    @DeleteMapping("/deleteCompany/{account}")
    public Result deleteCompany(@PathVariable String account)  {
        userService.deleteCompany(account);
        return Result.success("删除成功");
    }







}
